package com.jinxin.platform.cddjnhjcxtsysset.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomePrice;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysOrgInfo;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysUserVO;
import com.jinxin.platform.cddjnhjcxtsysset.mapper.CddjnhjcxtsyssetHomePriceMapper;
import com.jinxin.platform.cddjnhjcxtsysset.rpc.CddjnhjcxtsyssetRpc;
import com.jinxin.platform.cddjnhjcxtsysset.service.ICddjnhjcxtsyssetHomePriceService;
import com.jinxin.platform.cddjnhjcxtsysset.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-07-20
 * @Author: Lingnan
 **/
@Service
public class CddjnhjcxtsyssetHomePriceServiceImpl implements ICddjnhjcxtsyssetHomePriceService {
    @Autowired
    private CddjnhjcxtsyssetHomePriceMapper homePriceMapper;
    @Autowired
    private CddjnhjcxtsyssetRpc cddjnhjcxtsyssetRpc;

    @Override
    public CddjnhjcxtsyssetHomePrice selectOne(HashMap param) {
        return homePriceMapper.selectOne(param);
    }

    @Override
    public List<CddjnhjcxtsyssetHomePrice> list(HashMap param) {
        return homePriceMapper.list(param);
    }

    @Override
    public JsonResult saveOrUpdate(List<CddjnhjcxtsyssetHomePrice> records) {
        SysUserVO currentUser = cddjnhjcxtsyssetRpc.getCurrentUser();
        if(currentUser == null){
            return JsonResult.error("获取当前用户信息失败");
        }
        if (records == null || records.size() == 0){
            return JsonResult.error("参数错误，操作失败");
        }
        String orgId = currentUser.getOrgId();
        homePriceMapper.deleteByOrgId(orgId);
        List<CddjnhjcxtsyssetHomePrice> list = new ArrayList<>();
        records.forEach(record->{
            record.setId(Utils.getUuid());
            record.setOrgId(orgId);
            list.add(record);
        });
        int count = homePriceMapper.insertBatch(list);
        if(count > 0){
            return JsonResult.ok("保存成功","");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }
}
